package cn.ipanel.bigdata.boot.date

/**
 * Author: lzz
 * Date: 2022/3/24 8:47
 */
case class Minute (st: Long, et: Long) extends Date(st, et) {

  override def prev(n: Int = 1): Minute = Minute(st - Date.MINUTE_TIME * n)
  override def next(n: Int = 1): Minute = Minute(et + Date.MINUTE_TIME * n)
  def toDayIndex: Int = STime.toHour * 60 + STime.toMinute
  def toHourIndex: Int = STime.toMinute
  override def toString: String = s"Minute(${STime.toYYYYMMddHHmmss} To ${ETime.toYYYYMMddHHmmss})"
}

object Minute {

  def unapply(yyyyMMddHHmmss: String): Option[(Long, Long)] = {
    unapply(Time(yyyyMMddHHmmss, Time._yyyy_MM_dd_HH_mm_ss).time)
  }

  def unapply(time: Long): Option[(Long, Long)] = {
    val ca: java.util.Calendar = java.util.Calendar.getInstance();
    ca.setTime(new java.util.Date(time))
    ca.set(java.util.Calendar.SECOND, 0)
    ca.set(java.util.Calendar.MILLISECOND, 0)
    Some(ca.getTime.getTime, ca.getTime.getTime + Date.MINUTE_TIME - 1)
  }

  def apply(yyyyMMddHHmm: String): Minute = apply(Time(yyyyMMddHHmm, "yyyyMMddHHmm"))
  def apply(time: Time): Minute = apply(time.time)
  def apply(time: Long): Minute = time match {
    case Minute(st, et) => Minute(st, et)
    case _ => null
  }
}